﻿package plugin.roleN.controller
{
    import com.core.*;
    import com.greensock.*;
    import com.sh.game.display.*;
    import flash.events.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.roleN.model.*;
    import pluginfw.nio.*;
    import uiresource.roleui.*;

    public class NeiGongCtrl extends JingMaiUI
    {
        private var cfgNeili:Array;
        private var descArr:Array;
        private var neigongObj:Object;
        private var curLevel:int = 0;
        private var oldLevel:int = 0;
        private var _repeat:int = 0;
        private var oldExp:Number = 0;
        private var exp:Number = 0;
        private var oldNeiGong:int = 0;
        private var tweenMax:TweenMax;
        private var btnEffect:TweenMax;
        private var init:int = 0;
        private var oldlv:int = 0;
        private var isAdd:Boolean = false;
        private var num:int = 0;
        private var successMc:XMLMovieClip;
        private var successMc2:XMLMovieClip;
        private var expMc:XMLMovieClip;

        public function NeiGongCtrl()
        {
            this.cfgNeili = ConfigDictionary.data.neili;
            this.descArr = ["内功可抵消受到的部分伤害", "减伤越高，内功可抵消的伤害越多", "内功经验可通过杀怪获得"];
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            shuomlist.array = this.descArr;
            shuomlist.renderHandler = new Handler(this.descRender);
            expLabel.text = "0/0";
            yijianbtn.addEventListener(MouseEvent.CLICK, this.shengjiHandler);
            return;
        }// end function

        public function openRefresh() : void
        {
            if (!this.neigongObj)
            {
                this.neigongObj = ModelProxy.getValue("data", "neiGongCount");
            }
            this.showNeiGongInfo();
            return;
        }// end function

        public function showNeiGongInfo() : void
        {
            this.clearBtnEffect();
            this.curLevel = this.neigongObj.level;
            this.exp = this.neigongObj.exp;
            if (this.init == 0)
            {
                this.oldExp = this.exp;
                this.oldLevel = this.curLevel;
            }
            else
            {
                this.curLevel = this.oldLevel;
            }
            if (this.curLevel >= 100)
            {
                this.curLevel = 100;
                yijianbtn.disabled = true;
                yijianbtn.filters = null;
                expLabel.text = this.cfgNeili[this.curLevel].exp + "/" + this.cfgNeili[this.curLevel].exp;
                lvpower.width = 238;
            }
            else
            {
                expLabel.text = this.neigongObj.exp + "/" + this.cfgNeili[this.curLevel].exp;
                if (this.neigongObj.exp >= this.cfgNeili[this.curLevel].exp)
                {
                    lvpower.width = 238;
                    if (this.init == 0)
                    {
                        ngManImg.visible = true;
                        this.btnEffect = TweenMax.to(yijianbtn, 1, {glowFilter:{color:16763904, alpha:1, blurX:15, blurY:15, strength:2}, repeat:10000, yoyo:true});
                        this.showEffect();
                    }
                }
                else
                {
                    lvpower.width = this.neigongObj.exp / this.cfgNeili[this.curLevel].exp * 238;
                    ngManImg.visible = false;
                    this.playComplete1();
                }
            }
            if (UserData.getUserData("career") == 1)
            {
                neigongLabel.text = this.cfgNeili[this.curLevel].zsneili;
            }
            else if (UserData.getUserData("career") == 2)
            {
                neigongLabel.text = this.cfgNeili[this.curLevel].fsneili;
            }
            else
            {
                neigongLabel.text = this.cfgNeili[this.curLevel].dsneili;
            }
            jingmai.url = "cs2_roleN/jingmai" + this.cfgNeili[this.curLevel].pic + ".png";
            jianshangLabel.text = this.cfgNeili[this.curLevel].msshow / 100 + "%";
            this.showLv(this.curLevel);
            this.isAdd = true;
            return;
        }// end function

        public function updateNeiGongExp(param1:int) : void
        {
            var _loc_2:int = 0;
            if (this.curLevel > 100)
            {
                return;
            }
            if (this.init == 0)
            {
                if (!this.neigongObj)
                {
                    this.neigongObj = ModelProxy.getValue("data", "neiGongCount");
                    this.exp = this.neigongObj.exp;
                }
                _loc_2 = this.exp;
                this.showNeiGongInfo();
            }
            return;
        }// end function

        public function updateNeiGongCount(param1:Object) : void
        {
            this.showSJEffect();
            ngManImg.visible = false;
            this.clearBtnEffect();
            yijianbtn.filters = [];
            this.neigongObj = ModelProxy.getValue("data", "neiGongCount");
            this.curLevel = this.neigongObj.level;
            if (this.isAdd)
            {
                this._repeat = this.curLevel - this.oldLevel - 1;
            }
            else
            {
                this._repeat = this.curLevel - this.oldLevel - 1;
            }
            this.playComplete1();
            if (this._repeat >= 1)
            {
                lvpower.width = 0;
                jianshangLabel.text = this.cfgNeili[(this.oldLevel + 1)].msshow / 100 + "%";
                expLabel.text = String(this.oldExp - int(this.cfgNeili[this.oldLevel].exp)) + "/" + this.cfgNeili[(this.oldLevel + 1)].exp;
                jingmai.url = "cs2_roleN/jingmai" + this.cfgNeili[(this.oldLevel + 1)].pic + ".png";
                if (UserData.getUserData("career") == 1)
                {
                    neigongLabel.text = this.cfgNeili[(this.oldLevel + 1)].zsneili;
                }
                else if (UserData.getUserData("career") == 2)
                {
                    neigongLabel.text = this.cfgNeili[(this.oldLevel + 1)].fsneili;
                }
                else
                {
                    neigongLabel.text = this.cfgNeili[(this.oldLevel + 1)].dsneili;
                }
                this.showLv((this.oldLevel + 1));
                this.oldExp = this.oldExp - int(this.cfgNeili[this.oldLevel].exp);
                (this.oldLevel + 1);
                this.showTween();
            }
            else
            {
                this.glowEnd();
            }
            return;
        }// end function

        private function showTween() : void
        {
            this.tweenMax = TweenMax.to(lvpower, 1, {width:238, onComplete:function () : void
            {
                if (this.tweenMax)
                {
                    tweenMax = null;
                }
                var _loc_1:* = oldExp - int(cfgNeili[oldLevel].exp);
                if (oldLevel >= 100 || _loc_1 < 0)
                {
                    num = 0;
                    glowEnd();
                    return;
                }
                jianshangLabel.text = cfgNeili[(oldLevel + 1)].msshow / 100 + "%";
                expLabel.text = String(_loc_1) + "/" + cfgNeili[(oldLevel + 1)].exp;
                jingmai.url = "cs2_roleN/jingmai" + cfgNeili[(oldLevel + 1)].pic + ".png";
                if (UserData.getUserData("career") == 1)
                {
                    neigongLabel.text = cfgNeili[(oldLevel + 1)].zsneili;
                }
                else if (UserData.getUserData("career") == 2)
                {
                    neigongLabel.text = cfgNeili[(oldLevel + 1)].fsneili;
                }
                else
                {
                    neigongLabel.text = cfgNeili[(oldLevel + 1)].dsneili;
                }
                showLv((oldLevel + 1));
                (oldLevel + 1);
                isAdd = false;
                (num + 1);
                oldExp = oldExp - int(cfgNeili[oldLevel].exp);
                if (num < _repeat)
                {
                    lvpower.width = 0;
                    showTween();
                }
                else
                {
                    num = 0;
                    glowEnd();
                }
                return;
            }// end function
            });
            return;
        }// end function

        private function glowEnd() : void
        {
            lvpower.width = 0;
            this.playComplete1();
            var w:int;
            this.showLv(this.curLevel);
            if (this.neigongObj.exp >= this.cfgNeili[this.curLevel].exp || this.curLevel == 100)
            {
                w;
            }
            else
            {
                w = this.neigongObj.exp / this.cfgNeili[this.curLevel].exp * 238;
            }
            if (this.curLevel >= 100)
            {
                this.curLevel = 100;
                yijianbtn.disabled = true;
                yijianbtn.filters = null;
                expLabel.text = this.cfgNeili[this.curLevel].exp + "/" + this.cfgNeili[this.curLevel].exp;
            }
            else
            {
                yijianbtn.disabled = false;
                expLabel.text = this.neigongObj.exp + "/" + this.cfgNeili[this.curLevel].exp;
            }
            this.tweenMax = TweenMax.to(lvpower, 1, {width:w, onComplete:function () : void
            {
                if (this.tweenMax)
                {
                    tweenMax = null;
                }
                exp = neigongObj.exp;
                jianshangLabel.text = cfgNeili[curLevel].msshow / 100 + "%";
                jingmai.url = "cs2_roleN/jingmai" + cfgNeili[curLevel].pic + ".png";
                if (neigongObj.exp >= cfgNeili[curLevel].exp && curLevel < 100)
                {
                    showEffect();
                }
                else
                {
                    playComplete1();
                }
                if (UserData.getUserData("career") == 1)
                {
                    neigongLabel.text = cfgNeili[curLevel].zsneili;
                }
                else if (UserData.getUserData("career") == 2)
                {
                    neigongLabel.text = cfgNeili[curLevel].fsneili;
                }
                else
                {
                    neigongLabel.text = cfgNeili[curLevel].dsneili;
                }
                oldLevel = curLevel;
                oldExp = neigongObj.exp;
                init = 0;
                return;
            }// end function
            });
            return;
        }// end function

        private function showLv(param1:int) : void
        {
            img1.x = 163;
            img2.x = 181;
            img3.x = 199;
            if (param1 < 10)
            {
                var _loc_2:Boolean = false;
                img3.visible = false;
                img1.visible = _loc_2;
                img2.url = "png.common." + param1;
            }
            else if (param1 >= 10 && param1 < 100)
            {
                img1.visible = true;
                img3.visible = false;
                img2.url = "png.common." + param1 % 10;
                img1.url = "png.common." + Math.floor(param1 / 10);
                img1.x = 169;
                img2.x = 189;
            }
            else
            {
                var _loc_2:Boolean = true;
                img3.visible = true;
                img1.visible = _loc_2;
                img1.url = "png.common.1";
                img2.url = "png.common.0";
                img2.url = "png.common.0";
            }
            return;
        }// end function

        private function showSJEffect() : void
        {
            this.successMc = XMLMcManager.instance.getPluginMc("role", "ng_success1");
            if (this.successMc)
            {
                this.addChild(this.successMc);
                this.successMc.mouseChildren = false;
                this.successMc.mouseEnabled = false;
                this.successMc.x = 27;
                this.successMc.y = 374;
                this.successMc.playFromTo(0, (this.successMc.totalFrame - 1), new Handler(this.playComplete, [this.successMc]));
            }
            return;
        }// end function

        private function playComplete(param1:XMLMovieClip) : void
        {
            if (param1)
            {
                if (param1.parent)
                {
                    param1.parent.removeChild(param1);
                }
                param1.stop();
            }
            this.successMc2 = XMLMcManager.instance.getPluginMc("role", "ng_success2");
            if (this.successMc2)
            {
                this.addChild(this.successMc2);
                this.successMc2.mouseChildren = false;
                this.successMc2.mouseEnabled = false;
                this.successMc2.x = 24;
                this.successMc2.y = 55;
                this.successMc2.playFromTo(0, (this.successMc2.totalFrame - 1), new Handler(this.playComplete2, [this.successMc2]));
            }
            return;
        }// end function

        public function clearSuccessEffect() : void
        {
            if (this.successMc)
            {
                if (this.successMc.parent)
                {
                    this.successMc.parent.removeChild(this.successMc);
                }
                this.successMc.stop();
            }
            if (this.successMc2)
            {
                if (this.successMc2.parent)
                {
                    this.successMc2.parent.removeChild(this.successMc2);
                }
                this.successMc2.stop();
            }
            this.clearBtnEffect();
            return;
        }// end function

        private function playComplete2(param1:XMLMovieClip) : void
        {
            if (param1)
            {
                if (param1.parent)
                {
                    param1.parent.removeChild(param1);
                }
                param1.stop();
            }
            return;
        }// end function

        private function showEffect() : void
        {
            this.playComplete1();
            this.expMc = XMLMcManager.instance.getPluginMc("role", "ng_lizi");
            if (this.expMc)
            {
                panel.addChild(this.expMc);
                this.expMc.mouseChildren = false;
                this.expMc.mouseEnabled = false;
                this.expMc.x = 180 - 12;
                this.expMc.y = 480 - 90 - 384 - 32;
                this.expMc.play();
            }
            return;
        }// end function

        public function playComplete1() : void
        {
            if (this.expMc)
            {
                if (this.expMc.parent)
                {
                    this.expMc.parent.removeChild(this.expMc);
                }
                this.expMc.stop();
            }
            return;
        }// end function

        private function shengjiHandler(event:MouseEvent) : void
        {
            this.init = 1;
            if (this.exp < this.cfgNeili[this.curLevel].exp)
            {
                ModelProxy.change("movetips", "error", "内力经验不足");
                return;
            }
            var _loc_2:* = new Output();
            PluginManager.instance.sendToBufferServer("role", CMDRole.AskNeiGongUp, _loc_2);
            return;
        }// end function

        public function clearBtnEffect() : void
        {
            if (this.btnEffect)
            {
                this.btnEffect.kill();
            }
            yijianbtn.filters = [];
            return;
        }// end function

        private function descRender(param1:Component, param2:int) : void
        {
            var _loc_3:Label = null;
            if (param2 < shuomlist.length)
            {
                _loc_3 = param1.getChildByName("shuomingLabel") as Label;
                _loc_3.leading = 3;
                _loc_3.text = shuomlist.array[param2];
            }
            return;
        }// end function

    }
}
